package com.geek.novel.services

import com.geek.novel.common.Constant
import com.geek.novel.entity.AppTypeface
import com.geek.novel.entity.BookCrawler
import com.geek.novel.entity.CrawlerSource
import com.geek.novel.network.RepositoryProvider
import com.geek.novel.network.RslHandler
import com.geek.novel.repository.HttpRepository
import com.geek.novel.vo.*

/**
 * 版本相关Service
 */
class NovelService(rslHandler: RslHandler): BaseService(rslHandler) {

    private var repository: HttpRepository = RepositoryProvider.provideRepository()

    /**
     * 获取字体列表
     */
    fun getTypefaceList() {
        val observer = repository.getTypefaceList()
        compose(observer, Constant.HTTP_KEY_APP_GET_TYPEFACE_LIST, AppTypeface::class.java, Constant.HTTP_RESULT_TYPE_LIST)
    }

    /**
     * 获取首页推荐小说
     */
    fun getRecommendBooks() {
        val observer = repository.getRecommendBooks()
        compose(observer, Constant.HTTP_KEY_APP_GET_RECOMMEND_BOOK_LIST, BookStoreVo::class.java)
    }

    /**
     * 获取书籍详情
     */
    fun getBookDetail(bookId: String) {
        val observer = repository.getBookDetail(bookId)
        compose(observer, Constant.HTTP_KEY_APP_GET_BOOK_DETAIL, BookDetailVo::class.java)
    }

    /**
     * 根据作者id获取作者的所有书籍
     */
    fun getAuthorBooks(authorId: String, excludeBookId: String) {
        val observer = repository.getAuthorBooks(authorId, excludeBookId)
        compose(observer, Constant.HTTP_KEY_APP_GET_AUTHOR_BOOKS, AuthorBooksVo::class.java)
    }

    /**
     * 根据作者id获取作者的所有书籍
     */
    fun bookSearch(keyword: String, category: Int?, pager: Pager<Any>) {
        val observer = repository.bookSearch(keyword, category, pager)
        compose(observer, Constant.HTTP_KEY_APP_BOOK_SEARCH, BookSearchVo::class.java)
    }

    /**
     * 获取所有爬虫源
     */
    fun getCrawlerSourceList() {
        val observer = repository.getCrawlerSourceList()
        compose(observer, Constant.HTTP_KEY_APP_GET_CRAWLER_SOURCE_LIST, CrawlerSource::class.java, Constant.HTTP_RESULT_TYPE_LIST)
    }

    /**
     * 获取小说分类
     */
    fun bookCategories() {
        val observer = repository.bookCategories()
        compose(observer, Constant.HTTP_KEY_APP_GET_BOOK_CATEGORIES, BookCategoriesVo::class.java)
    }

    /**
     * 根据分类信息获取小说列表
     */
    fun queryBookByCategory(category: String, pager: Pager<Any>) {
        val observer = repository.queryBookByCategory(category, pager)
        compose(observer, Constant.HTTP_KEY_APP_GET_BOOK_BY_CATEGORY, BookSearchVo::class.java)
    }

    /**
     * 获取书籍爬虫源列表
     */
    fun getBookCrawlerList(bookId: String) {
        val observer = repository.getBookCrawlerList(bookId)
        compose(observer, Constant.HTTP_KEY_APP_GET_BOOK_CRAWLER_LIST, BookCrawler::class.java, Constant.HTTP_RESULT_TYPE_LIST)
    }

    /**
     * 用户加收藏
     */
    fun addUserCollect(bookId: String) {
        val observer = repository.addUserCollect(bookId)
        compose(observer, Constant.HTTP_KEY_APP_USER_COLLECT_ADD)
    }

    /**
     * 用户删除收藏
     */
    fun removeUserCollect(bookId: String) {
        val observer = repository.removeUserCollect(bookId)
        compose(observer, Constant.HTTP_KEY_APP_USER_COLLECT_REMOVE)
    }

    /**
     * 获取用户收藏列表
     */
    fun queryUserCollected(pager: Pager<Any>) {
        val observer = repository.queryUserCollected(pager)
        compose(observer, Constant.HTTP_KEY_APP_USER_COLLECT_QUERY, BookUserCollectVo::class.java)
    }

    /**
     * 书籍点击数+1
     */
    fun addVisitCount(bookId: String) {
        val observer = repository.addVisitCount(bookId)
        compose(observer, Constant.HTTP_KEY_APP_USER_COLLECT_ADD_VISIT_COUNT)
    }

    /**
     * 书籍阅读数+1
     */
    fun addReadCount(bookId: String) {
        val observer = repository.addReadCount(bookId)
        compose(observer, Constant.HTTP_KEY_APP_USER_COLLECT_ADD_READ_COUNT)
    }

    /**
     * 书籍阅读数-1
     */
    fun minusReadCount(bookId: String) {
        val observer = repository.minusReadCount(bookId)
        compose(observer, Constant.HTTP_KEY_APP_USER_COLLECT_MINUS_READ_COUNT)
    }

    /**
     * 热搜词
     */
    fun queryBookSearchHotKey() {
        val observer = repository.queryBookSearchHotKey()
        compose(observer, Constant.HTTP_KEY_APP_QUERY_BOOK_SEARCH_HOT_KEY, BookSearchHotKeyVo::class.java, Constant.HTTP_RESULT_TYPE_LIST)
    }

}
